#include <iostream>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/types.h>
#include <time.h>
#include "prompt.hpp"
#include "log.hpp"
#include "fstring.hpp"
#include "user.hpp"

#define BUFFSIZE 100000
		
#define CMAX 990

using namespace std;
/*
 * Cette classe représente le serveur
 */
class Server : public Prompt
{
public:
	Server(int port = 1888, FString logDir = "/var/log/01lut/");
	~Server();

	void start();
	void stop();

private:
	char buff[BUFFSIZE];
	bool activate;

	Log *logMsg;
	Log *logSvr;
	
	int lport;
	int sockServer;
	sockaddr_in serverAddress;

	int highestSock;
	fd_set socksToListen;
//	int clients[CMAX];
	User* clients[CMAX];
	int nbClients;

	
	void initList();
	void runLoop();
	void readSocks();
	void forwardFrom(int from, FString str);
	void forward(FString str);
	void handleRequest(User *cli, char *request);

	void sendMsg(User *cli, FString msg);
	int checkSocket(int id);
	User* findClient(int id);
	
	void alertClients();

};
